package com.zzxy.spring5.factory;


import com.zzxy.spring5.aspect.MyAspect;
import com.zzxy.spring5.service.impl.StudentServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyBeanFactory {
    public static IStudentService createStudentServiceProxy() {
//1.target class
        StudentServiceImpl studentService = new StudentServiceImpl();
//2.advice
        MyAspect aspect = new MyAspect();
//3.weaving based jdk
        IStudentService studentServiceProxy = (IStudentService) Proxy.newProxyInstance(MyBeanFactory.class.getClassLoader(), studentService.getClass().getInterfaces(), new InvocationHandler()
        {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                aspect.before();
                Object obj = method.invoke(studentService, args);
                aspect.after();
                return obj;
            }
        });
        return studentServiceProxy;
    }

    private static class IStudentService {
    }
}
